package top.yuzt.animalserver.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.yuzt.animalserver.entity.*;
import top.yuzt.animalserver.mapper.MedicalMapper;
import top.yuzt.animalserver.service.impl.AnimalServiceImpl;
import top.yuzt.animalserver.service.impl.MedicalServiceImpl;
import top.yuzt.animalserver.service.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuzt
 * @since 2024-05-17
 */
@RestController
@RequestMapping("/medical")
public class MedicalController {

    @Autowired
    private MedicalServiceImpl medicalService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private AnimalServiceImpl animalService;
    @Autowired
    private MedicalMapper medicalMapper;
    @SaIgnore
    @GetMapping("/getlist")
    public List<PackMedical> getuserlist(){
        List<PackMedical> packMedicalList =  new ArrayList<>();
        List<Medical> medicalList = medicalService.list();
        for (Medical x : medicalList){
            Animal animal = animalService.getById(x.getAnimalId());
            if(animal != null){
                String keepername = userService.getById(x.getKeeperId()).getNickname();
                if(x.getDoctorId() == null){
                    packMedicalList.add(new PackMedical(x,animal,keepername));
                }else {
                    String doctorname = userService.getById(x.getDoctorId()).getNickname();
                    packMedicalList.add(new PackMedical(x,animal,keepername,doctorname));
                }
            }
        }
        return packMedicalList;
    }
    //返回用户对应的医疗记录
    @SaIgnore
    @GetMapping("/getlist/byuserid")
    public List<PackMedical> getuserlistByID(Integer id){
        if(Integer.parseInt(userService.getById(id).getRole()) > 2){
            return getuserlist();
        }else {
            List<PackMedical> packMedicalList = new ArrayList<>();
            for (Medical x : medicalService.list()){
                Animal animal = animalService.getById(x.getAnimalId());
                if(animal != null){
                    String keepername = userService.getById(x.getKeeperId()).getNickname();
                    if(x.getDoctorId() == id || x.getKeeperId() == id){
                        if(x.getDoctorId() == null){
                            packMedicalList.add(new PackMedical(x,animal,keepername));
                        }else {
                            String doctorname = userService.getById(x.getDoctorId()).getNickname();
                            packMedicalList.add(new PackMedical(x,animal,keepername,doctorname));
                        }
                    }
                }
            }
            return packMedicalList;
        }
    }

    //新增
    @SaIgnore
    @PostMapping("/save")
    public boolean savemedical(@RequestBody Medical medical){
        return medicalService.save(medical);
    }
    //更新
    @SaIgnore
    @PostMapping("/update")
    public boolean updatemedical(@RequestBody Medical medical){

        return medicalService.updateById(medical);
    }
    //删除
    @SaIgnore
    @PostMapping("/remove")
    public boolean removemedical(@RequestBody Medical medical){
        medicalService.updateById(medical);
        return  medicalMapper.setNull(medical.getId()).isEmpty();
    }
}
